<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        :option="option"
        :table-loading="loading"
        @on-load="listdata"
        @refresh-change="listdata"
        @search-change="searchChange"
        :page.sync="page"
      >
        <!-- 自定义按钮 -->
        <template slot="menuLeft">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click.stop="$refs.ChannelForm.open()"
          >
            新 增
          </el-button
          >
        </template>

        <template slot-scope="{ row, index }" slot="menu">
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            @click.stop="rowEdit(row)"
          >编辑
          </el-button
          >

          <el-popconfirm
            style="padding: 0 10px"
            title="确定删除？"
            @confirm="del(row, index)"
          >
            <el-button slot="reference" type="text" size="small"
            >删除
            </el-button
            >
          </el-popconfirm>
        </template>
      </avue-crud>
    </basic-container>
    <ChannelForm ref="ChannelForm" @refreshDataList="listdata"></ChannelForm>
  </div>
</template>

<script>
  import {option} from "@/const/crud/houxin/channelTree";
  import {delObj, fetchList} from "@/api/houxin/channelTree";
  import ChannelForm from "./channel-form.vue";

  export default {
    components: {ChannelForm},
    data() {
      return {
        option: option,
        data: [],
        loading: true,
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条
        },
      };
    },
    methods: {
      listdata() {
        this.loading = true;
        fetchList({
          pageIndex: this.page.currentPage,
          pageSize: this.page.pageSize,
        }).then((r) => {
          console.log(r.data.data.list);
          this.data = r.data.data.list;
          this.page.total = r.data.data.total;
          this.loading = false;
        });
      },
      searchChange(form, done) {
        fetchList({
          pageIndex: this.page.currentPage,
          pageSize: this.page.pageSize,
          ...form,
        }).then((r) => {
          this.data = r.data.data.list;
          this.page.total = r.data.data.total;
          this.loading = false;
        });
        done();
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      rowEdit(row) {
        this.$refs["ChannelForm"].edit(row);
      },
      del(row, index) {
        delObj({channelIds: [row.id]}).then((r) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.listdata();
        });
      },
    },
  };
</script>

<style>
</style>
